<?php
//上游订单确认
//反状态（OrderId：订单号（同取单接口fuluOrderId），OrderStatus：订单状态（1：成功 2：失败），ChargeResult：充值描述，RealCost：成本（外部可随意），amount_type：固定RMB，SubstituteAccount：固定“外部商户”，ChannelAccountId：固定“外部商户”）
function callBackOrderStatus($orderId,$orderStatus,$time,$str='Serge#57$768rtv0'){

    $sign = strtoupper(md5($orderId.$orderStatus.$time.$str));

    if($orderStatus == 1){
        $postData = "OrderId=".$orderId."&OrderStatus=".$orderStatus."&Description=充值成功&RealCost=0&Currency=RMB&SubstituteAccount=外部商户042&ChannelAccountId=外部商户042&Ts=".$time."&Sign=".$sign;
    }else{
        $postData = "OrderId=".$orderId."&OrderStatus=".$orderStatus."&Description=充值失败&RealCost=0&Currency=RMB&SubstituteAccount=外部商户042&ChannelAccountId=外部商户042&Ts=".$time."&Sign=".$sign;
    }


    $url = "http://interface.qsios.cn:8136/actionapi/Order/CallBackOrderStatus";

    $res = curlPost($url,$postData);

    return $res;
}

function curlPost($url,$post_data = 0)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

function curlGet($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

function checkSign($data){
    $checksign = md5($data['key'].$data['orderno'].$data['chargeaccount'].$data['type'].$data['goods'].$data['buytime'].$data['callbackUrl'].$data['secret']);
    $checksign1 = $data['key'].$data['orderno'].$data['chargeaccount'].$data['type'].$data['goods'].$data['buytime'].$data['callbackUrl'].$data['secret'];

    if($checksign == $data['sign']){
        return 1;
    }else{
        return 0;
    }
}

function checkSignGetOrderInfo($data){
    $checksign = md5($data['key'].$data['orderno'].$data['chargeaccount'].$data['secret']);
//    return $checksign;
    if($checksign == $data['sign']){
        return 1;
    }else{
        return 0;
    }
}

function callBack($code,$result = []){
    switch ($code){
        case    0:
            $message = "创建订单成功";
            break;
        case    200:
            $message = "订单查询成功";
            break;
        case    1001:
            $message = "签名失败";
            break;
        case    1002:
            $message = "无权限";
            break;
        case    1003:
            $message = "订单号异常,订单已存在";
            break;
        case    1004:
            $message = "缺少必要的参数";
            break;
        case    1005:
            $message = "订单不存在";
            break;
    }
    $data =
        [
            "code"      =>      $code,
            "message"   =>      $message,
            "result"    =>      $result
        ];

    return json_encode($data,JSON_UNESCAPED_UNICODE);
}
